package com.baizhi.service.impl;

import com.baizhi.dao.TagDAO;
import com.baizhi.dto.TagDTO;
import com.baizhi.entity.Tag;
import com.baizhi.service.TagService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class TagServiceImpl extends ServiceImpl<TagDAO, Tag> implements TagService {

    @Autowired
    private TagDAO tagDAO;

    @Override
    public List<Tag> queryAll() {
        List<Tag> tags = tagDAO.selectList(null);
        return tags;
    }

    @Override
    public Tag add(TagDTO tagDTO) {
        Date date = new Date();
        Tag tag = new Tag();
        tag.setCreateDate(new Date());
        tag.setName(tagDTO.getName());
        tag.setType(tagDTO.getType());
        int insert = tagDAO.insert(tag);

        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getName, tagDTO.getName());
        queryWrapper.eq(Tag::getType, tagDTO.getType());
        Tag tag1 = tagDAO.selectOne(queryWrapper);
        return tag1;
    }

    @Override
    public Tag queryById(Long id) {
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Tag::getId,id);
        Tag tag = tagDAO.selectOne(queryWrapper);
        return tag;
    }

}
